package com.linglingC.controller;

import com.linglingC.pojo.*;
import com.linglingC.service.ResidentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
public class ResidentContrroller {

    @Autowired
    private ResidentService residentService;


    @GetMapping("/residentFindAll")
    public Result residentFindAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize
    ){
        log.info("住户表分页查询: {} {}",page,pageSize);

        PageBean pageBean=residentService.findAll(page,pageSize);

        return Result.success(pageBean);
    }

    @GetMapping("/residentSelect")
    public Result empSelect(String key,String value,@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("住户表条件: 字段:{} 值:{}",key,value );

        PageBean pageBean=residentService.select(key,value,page,pageSize);

        return  Result.success(pageBean);
    }

    @PostMapping("/residenInsert")
    private Result insertEmp(@RequestBody Resident resident){
        log.info("添加住户: {}",resident);
        residentService.addResident(resident);
        return Result.success();
    }

    @DeleteMapping("/residenDelete/{ID}")
    private Result empDelete(@PathVariable Integer ID){
        log.info("删除单一住户: ID: {}",ID);
        residentService.deleteOneResident(ID);
        return Result.success("删除成功");
    }

    @PutMapping("/residenEdit")
    public Result editEmp(@RequestBody Resident resident){
        log.info("修改住户信息: {}",resident);
        residentService.editResident(resident);
        return Result.success();
    }

    @GetMapping("/residentNameID")
    public Result residentNameID(){
        log.info("查询住户名称和ID");

        List<ResidentNameId> r=  residentService.selectNameID();

        return Result.success(r);
    }
}
